﻿using System;
using DL.PhotoCollage.Core;

namespace DL.PhotoCollage.Wpf.Presentation
{
    internal class ImageDisplayPresenter
    {
        private const int maximumAngle = 15;

        private readonly ControllerBase controller;

        private double maximumSizeDiu;

        private double dpiScale;

        public ImageDisplayPresenter(ControllerBase applicationController)
        {
            this.controller = applicationController;
        }

        public double DpiScale 
        { 
            get { return this.dpiScale; }
        }

        public bool IsGrayscale
        {
            get { return this.controller.Configuration.IsGrayscale; }
        }

        public double MaximumSizeDeviceIndependentUnits
        {
            get { return this.maximumSizeDiu; }
        }

        public int MaximumSizePixels
        {
            get { return this.controller.Configuration.MaximumSize; }
        }

        public bool ShowBorders
        {
            get { return this.controller.Configuration.ShowPhotoBorder; }
        }

        public int GetRandomAngle()
        {
            return this.controller.GetRandomNumber(-maximumAngle, maximumAngle);
        }

        public void CalculateDpiScale(double imageDpi)
        {
            this.dpiScale = imageDpi / 96;
            this.maximumSizeDiu = this.MaximumSizePixels / this.dpiScale;
        }

        public bool DoesImageNeedScaling(double height, double width)
        {
            return height > this.maximumSizeDiu
                || width > this.maximumSizeDiu
                || this.dpiScale > 1;
        }

        public ImageOrientation GetImageOrientation(double height, double width)
        {
            return height > width
                ? ImageOrientation.Portrait
                : ImageOrientation.Landscape;
        }

        public double GetLandscapeScale(double width)
        {
            return (this.maximumSizeDiu / width) * this.dpiScale;
        }

        public double GetPortraitScale(double height)
        {
            return (this.maximumSizeDiu / height) * this.dpiScale;
        }

        public void HandleError(Exception ex)
        {
            this.controller.HandleError(ex);
        }
    }
}
